/**
 * @Copyright: Interactive Business Solution Pte Ltd, Singapore
 * @Author: Dolita, 2007-12-24
 * @Version: 071224.001
 * Data access object of Hotel model
 */
package com.ibsol.database
{
	import com.ibsol.model.Hotel;
	
	import flash.data.SQLConnection;
	import flash.data.SQLStatement;
	import flash.events.SQLErrorEvent;
	import flash.events.SQLEvent;
	
	import mx.collections.ArrayCollection;
	import mx.controls.Alert;
	
	public class HotelDAO
	{
		private var _tourixDBConn:SQLConnection;
		/**
		 * Default constructor of Hotel DAO, get database connection
		 */
		public function HotelDAO()
		{
			_tourixDBConn = DBConnManager.getConnection("tourix");
		}
		/**
		 * Insert data into hotel table
		 * hotel(id, cityid, rating, desp, offlineOperator, updateTime)
		 * @param: hotel:Hotel				hotel item object which will be populating hotel table
		 * 		   resultHandler:Function	result function adapter
		 * 		   faultHandler:Function	fault function adapter
		 * @return: void
		 */
		public function insertHotel(hotel:Hotel, resultHandler:Function = null, faultHandler:Function = null):void
		{
			var sqlStr:SQLStatement = new SQLStatement();
			sqlStr.sqlConnection = _tourixDBConn;
			sqlStr.text = "INSERT INTO hotel (id, cityid, rating, desp, offlineOperator, updateTime) " + 
						"VALUES (" + hotel.id + "," + hotel.cityid + ", " + hotel.rating + ", '" + hotel.desp + 
						"', 'NEW', 'T')";
			sqlStr.addEventListener(SQLEvent.RESULT,
				function(event:SQLEvent):void
				{
					if(resultHandler != null)
						resultHandler.call(this);
				});
			sqlStr.addEventListener(SQLErrorEvent.ERROR, faultHandler == null ? sqlErrorHandler : faultHandler);
			sqlStr.execute();
		}							
		/**
		 * Load flight according to city id
		 * @param:	cityid:int				city id, where the hotels are located
		 * 			resultHandler:Function	result function adapter, return hotel array collection
		 * 			faultHandler:Function	fault function adapter
		 * @return: void
		 */
		public function loadHotelsByCity(cityid:int, resultHandler:Function, faultHandler:Function = null):void
		{
			var sqlStr:SQLStatement = new SQLStatement();
			sqlStr.sqlConnection = _tourixDBConn;
			sqlStr.text = "SELECT * FROM hotel WHERE cityid = " + cityid + " AND offlineOperator != 'DEL'";
			sqlStr.addEventListener(SQLEvent.RESULT,
				function (event:SQLEvent):void
				{
					resultHandler.call(this, typeArray(sqlStr.getResult().data));
				});
			sqlStr.addEventListener(SQLErrorEvent.ERROR, faultHandler == null ? sqlErrorHandler : faultHandler);
			sqlStr.execute();
		}
		/**
		 * Updates a hotel record in database. For different level of user, there are different options.
		 * hotel(id, cityid, rating, desp, offlineOperator, updateTime)
		 * @param:	hotel:Hotel				hotel object which will be updated
		 * 			resultHandler:Function	result handler function
		 * 			faultHandler:Function	fault handler function
		 * @return: void
		 */	
		public function updateHotel(hotel:Hotel, resultHandler:Function = null, faultHandler:Function = null):void
		{
			var sqlStr:SQLStatement = new SQLStatement();
			sqlStr.sqlConnection = _tourixDBConn;
			sqlStr.text = "UPDATE hotel SET cityid=" + hotel.cityid + ",rating=" + hotel.rating + ", desp='" + hotel.desp + 
					"',offlineOperator = 'EDIT',updateTime='T' WHERE id = " + hotel.id;
			sqlStr.addEventListener(SQLEvent.RESULT,
				function(event:SQLEvent):void
				{
					if(resultHandler != null)
						resultHandler.call(this);
				});
			sqlStr.addEventListener(SQLErrorEvent.ERROR, faultHandler == null ? sqlErrorHandler : faultHandler);
			sqlStr.execute();
		}
		/**
		 * Permanently delete hotel from database, whose offlineOperator is set to "DEL". If user wants to delete hotel, 
		 * user just updates offlineOperator to "DEL".
		 * @param:	resultHandler:Function		result handler adapter
		 * 			faultHandler:Function		fault handler adapter
		 * @return:	void
		 */
		public function clearDeletedHotels(resultHandler:Function = null, faultHandler:Function = null):void
		{
			var sqlStr:SQLStatement = new SQLStatement();
			sqlStr.sqlConnection = _tourixDBConn;
			sqlStr.text = "DELETE FROM hotel WHERE offlineOperator = 'DEL'";
			sqlStr.addEventListener(SQLEvent.RESULT,
				function (event:SQLEvent):void
				{
					if(resultHandler != null)
						resultHandler.call(this);
				});
			sqlStr.addEventListener(SQLErrorEvent.ERROR, faultHandler == null ? sqlErrorHandler : faultHandler);
			sqlStr.execute();
		}
		/**
		 * Sets offline operator to "", this function called after synchronization with server
		 * @param:	resultHandler:Function		result handler adapter
		 * 			faultHandler:Function		fault handler adapter
		 * @return: void
		 */
		public function resetOfflineOperator(resultHandler:Function = null, faultHandler:Function = null):void
		{
			var sqlStr:SQLStatement = new SQLStatement();
			sqlStr.sqlConnection = _tourixDBConn;
			sqlStr.text = "UPDATE hotel SET offlineOperator=''";
			sqlStr.addEventListener(SQLEvent.RESULT,
				function (event:SQLEvent):void
				{
					if(resultHandler != null)
						resultHandler.call(this);
				});
			sqlStr.addEventListener(SQLErrorEvent.ERROR, faultHandler == null ? sqlErrorHandler : faultHandler);
			sqlStr.execute();
		}
		/**
		 * Converts database object to Hotel class
		 * @param:	o:Object	database return object
		 * @return:	Hotel		Hotel object after conversion
		 */
		private function typeObject(o:Object):Hotel
		{
			var h:Hotel = new Hotel();
			h.id = o.id;
			h.cityid = o.cityid;
			h.rating = o.rating;
			h.desp = o.desp;
			h.offlineOperator = o.offlineOperator;
			h.updateTime = o.updateTime;
			
			return h;
		}
		/**
		 * Converts array object to Hotel array collection
		 * @param: a:Array array of objects returned by SQL
		 * @return: ArrayCollection of Hotel after conversion from Array
		 */
		private function typeArray(a:Array):ArrayCollection
		{
			if (a==null) return null;
			var ac:ArrayCollection = new ArrayCollection();
			for (var i:int; i<a.length; i++)
			{
				ac.addItem(typeObject(a[i]));
			}
			return ac;
		}
		/**
		 * Default SQL error handler, if above functions' fault handlers are null, this error handler is applied
		 * @param: event:SQLError	SQL Error event will be handled if there is an error
		 * @return: void
		 */
		private function sqlErrorHandler(event:SQLErrorEvent):void
		{
			mx.controls.Alert.show(event.text, "Error");
			trace(event.text);
		}
	}
}